﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Templates.SharpParts.Shells
{
	public partial class TempateCollectionShell : T4TemplateBase
	{
		public override void BeforeToString()
		{
			StringBuilder sb = new StringBuilder();

			if (Collection != null)
			{
				var col = Collection.Where(x => x != null).Distinct().ToArray();
				foreach (var template in col)
				{
					AddBlockId(template.Id);
					sb.Append(template.ToString());
					CopyNamespace(template);
				}
			}

			Text = sb.ToString();
		}

		public IEnumerable<T4TemplateBase> Collection { get; set; }

		public string Text { get; private set; }

		public override T4TemplateBase GetBlockById(Guid blockId)
		{
			if (Collection != null)
				foreach (var result in Collection.Select(templ => templ.GetBlockById(blockId)).Where(result => result != null))
				{
					return result;
				}

			return base.GetBlockById(blockId);
		}
	}
}
